import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap, asyncRouterMap } from '@/config/router.config'
import storge from 'store'
// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

// 动态生成路由 这里改权限设灰度
const parseRoute = () => {
  // const resRouteMap = asyncRouterMap,
  const menu = storge.get('menu')
  // console.log('menu', menu)
  // // const _children = []
  // asyncRouterMap[0].children.forEach((item, index) => {
  //   if (menu && menu.indexOf(item.gray) === -1) { asyncRouterMap[0].children[index] = undefined } else if (item.children.length > 0) {
  //     item.children.forEach((item, index) => {
  //       if (item && menu.indexOf(item.gray) === -1) { item[index] = undefined }
  //     })
  //   }
  // })
  // const newRoute = [Object.assign({}, asyncRouterMap[0], { children: [] }), asyncRouterMap[1]]
  // console.log(asyncRouterMap)

  // asyncRouterMap[0].children.forEach(item => {
  //   if (item) { newRoute[0].children.push(item) }
  //   const _subRoute = []
  //   console.log(item)
  //   if (item && item.children) {
  //     item.children.forEach(item => {
  //         if (item) { _subRoute.push(item) }
  //     })
  //     item.children = _subRoute
  //   }
  // })
  // console.log(asyncRouterMap, newRoute)
  // newRoute[0].children = [newRoute[0].children[0]]
  // console.log(asyncRouterMap(menu))
  const _route = asyncRouterMap(menu)
  return _route
}

export default new Router({
  mode: 'hash',
  routes: constantRouterMap.concat(parseRoute())
})
